<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="小区名称" prop="premiseName">
        <el-input
          v-model="queryParams.premiseName"
          placeholder="请输入小区名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租客姓名" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租客姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款单号" prop="receiptOrderNo">
        <el-input
          v-model="queryParams.receiptOrderNo"
          placeholder="请输入收款单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收款状态" prop="skStatus">
        <el-select v-model="queryParams.skStatus" placeholder="请选择收款状态" clearable size="small">
          <el-option v-for="dict in skStatus" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="账单类型" prop="billType">
        <el-select v-model="queryParams.billType" placeholder="请选择账单类型" clearable size="small">
          <el-option v-for="dict in billType" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="租房合同编号" prop="contractNo">
        <el-input
          v-model="queryParams.contractNo"
          placeholder="请输入承租房合同编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应收日期" prop="collectionDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.collectionDate"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="-----" prop="endCollectionDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.endCollectionDate"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="实收日期" prop="realCollectionDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.realCollectionDate"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="-----" prop="endRealCollectionDate">
        <el-date-picker clearable size="small" style="width: 200px"
                        v-model="queryParams.endRealCollectionDate"
                        type="date"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div>
      <el-radio-group v-model="queryParams.receiptType" @change="handleQuery">
        <el-radio-button label="1" >房租收款计划</el-radio-button>
        <el-radio-button label="2">租务收款计划</el-radio-button>
      </el-radio-group>
    </div><br/>

    <div v-show="queryParams.receiptType==1">
      <span style="font-weight: bold">应收金额总计：{{amountReceivable}}元</span><br/><br/>
      <span>实收金额总计:{{amountCollected}}元</span>
    </div><br/>

    <el-table v-loading="loading" :data="planList">
      <el-table-column label="租房合同编号" align="center" prop="contractNo" />
      <el-table-column label="收款单号" align="center" prop="receiptOrderNo" />
      <el-table-column label="承租房间" align="center" prop="roomName" />
      <el-table-column label="租客姓名" align="center" prop="tenantName" />
      <el-table-column label="小区名称" align="center" prop="premiseName" />
      <el-table-column label="支付金额" align="center" prop="paymentAmount" />
      <el-table-column label="选择账期" align="center" prop="receiptNumber">
        <template slot-scope="scope">
          <span>第{{scope.row.receiptNumber}}期</span>
        </template>
      </el-table-column>
      <el-table-column label="收款状态" align="center" prop="skStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.skStatus==1">未收款</span>
          <span v-else-if="scope.row.skStatus==2">已收款</span>
          <span v-else-if="scope.row.skStatus==3">已中止</span>
          <span v-else>已冻结</span>
        </template>
      </el-table-column>
      <el-table-column label="账单类型" align="center" prop="billType">
        <template slot-scope="scope">
          <span v-if="scope.row.billType==1">分期变更</span>
          <span v-else-if="scope.row.billType==2">租约保理变更</span>
          <span v-else-if="scope.row.billType==3">退租变更</span>
          <span v-else>普通账单</span>
        </template>
      </el-table-column><br/>
      <el-table-column label="预定收款日期" align="center" prop="collectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.collectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际收款日期" align="center" prop="realCollectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realCollectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payMethod">
        <template slot-scope="scope">
          <span v-if="scope.row.payMethod==1">快捷支付</span>
          <span v-else-if="scope.row.payMethod==2">支付宝</span>
          <span v-else-if="scope.row.payMethod==3">微信</span>
          <span v-else-if="scope.row.payMethod==4">现金</span>
          <span v-else-if="scope.row.payMethod==5">银行卡转账</span>
          <span v-else-if="scope.row.payMethod==6">POS机刷卡</span>
          <span v-else>其他</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operationUser" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/financing/receiptPlan";

export default {
  name: "Plan",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: 1,
        skStatus: null,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        endCollectionDate:null,
        endRealCollectionDate:null,
      },
      //应收金额
      amountReceivable:null,
      //实收金额
      amountCollected:null,
      //收款状态
      skStatus:[],
      //账单类型
      billType:[],
    };
  },
  created() {
    this.getList();
    this.getDicts("financing_sk_status").then(response => {
      this.skStatus = response.data;
    });
    this.getDicts("financing_bill_type").then(response => {
      this.billType = response.data;
    });
  },
  methods: {
    /** 查询收款计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.data.receiptPlanVOList.rows;
        this.total = response.data.receiptPlanVOList.total;
        this.amountReceivable = response.data.receiptPlanVO.paymentAmount;
        this.amountCollected = response.data.receiptPlanVO.realPaymentAmount;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        receiptOrderNo: null,
        businessId: null,
        contractId: null,
        contractNo: null,
        contractType: null,
        tTerminationId: null,
        premiseId: null,
        premiseName: null,
        roomId: null,
        roomCode: null,
        roomName: null,
        ownerId: null,
        ownerName: null,
        tenantId: null,
        tenantName: null,
        receiptType: null,
        skStatus: 0,
        billType: null,
        itemType: null,
        otherItemName: null,
        paymentAmount: null,
        realPaymentAmount: null,
        withReceiptPlanId: null,
        paymentDaysBegin: null,
        paymentDaysEnd: null,
        collectionDate: null,
        realCollectionDate: null,
        receiptNumber: null,
        payMethod: null,
        otherPayMethod: null,
        payFlowNo: null,
        rentalPrice: null,
        tax: null,
        operationUser: null,
        remark: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null,
        endCollectionDate:null,
        endRealCollectionDate:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除收款计划编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPlan(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/plan/export', {
        ...this.queryParams
      }, `system_plan.xlsx`)
    }
  }
};
</script>
